package com.qst.service.ssm;

import com.qst.ExamException;
import com.qst.dao.DAOFactory;
import com.qst.dao.QuestionDao;
import com.qst.entity.AssessmentType;
import com.qst.mapper.AssessmentMapper;
import com.qst.mapper.QuestionMapper;
import com.qst.service.AssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@SuppressWarnings("all")
public class AssessmentServiceSsmImpl implements AssessmentService {

    @Autowired
    AssessmentMapper assessmentMapper;
    @Autowired
    QuestionMapper questionMapper;


    @Override
    public List<AssessmentType> findAllAssessment() {
        return assessmentMapper.findAll();
    }

    @Override
    public AssessmentType findAssessmentById(int id) {
        return assessmentMapper.findById(id);
    }

    @Override
    public void deleteAssessment(int id) {
        if (questionMapper.findCountByAssessment(id) > 0) {
            throw new ExamException("本考核类型已有试题，不能删除");
        }
        assessmentMapper.delete(id);
    }

    @Override
    public void saveAssessment(AssessmentType assessment) {
        AssessmentType temp = assessmentMapper.findByTitle(assessment.getTitle());
        if (temp != null) {
            throw new ExamException("考核类型已存在");
        }
        assessmentMapper.insert(assessment);
    }

    @Override
    public void updateAssessment(AssessmentType assessment) {
        AssessmentType temp = assessmentMapper.findByTitle(assessment.getTitle());
        if (temp != null && temp.getId() != assessment.getId()) {
            throw new ExamException("考核类型重复");
        }
        assessmentMapper.update(assessment);
    }
}
